<?php
   session_start();
   $cart_arr=isset($_SESSION["cart"])?$_SESSION["cart"]:'';


   $Date=date('d/m/Y',time());
   $user_name=$_SESSION["user"]['user_name'];
   $Address=$_SESSION["user"]['address'];
   $Mobile=$_SESSION["user"]['tel'];
   $eamil=$_SESSION["user"]['eamil'];
   $OID=date('YmdHis',time());


?>
<?php

if(!empty($cart_arr)){


    $table_header=<<<"HTML"
         <table style="border-collapse: collapse;margin-bottom: 20px;">
          <tr>   
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Purchase Date</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Name</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Address</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Mobile</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Email</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">ID</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">OID</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Quantity</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Unit Price</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Subtotal</th>
         </tr>
HTML;

    echo $table_header;

    //追加(append)方式打开02.txt文件，文件不在会自动创建
    $fp = fopen('./order.txt','a');

    foreach($cart_arr as $k=>$arr){
        $price = $arr['price'];
        $quantity = $arr['quantity'];
        $id = $arr['id'];
        $total_price=$price*$quantity;

        $order['date']=$Date;
        $order['user_name']=$user_name;
        $order['Address']=$Address;
        $order['Mobile']=$Mobile;
        $order['eamil']=$eamil;
        $order['id']=$id;
        $order['OID']=$OID;
        $order['quantity']=$quantity;

        $order['price']=$price;
        $order['total_price']=$total_price;

        $data='';
        foreach ($order as $v){
            $data.=$v."   ";
        }
        $data="\r\n".$data;
        fwrite($fp,$data);

        $arr_html= <<<"HTML"

         <tr>
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$Date</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$user_name</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$Address</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$Mobile</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$eamil</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$id</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$OID</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$quantity</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$price</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$%1.2f</td> 
         </tr> 
  
HTML;

        printf($arr_html,$total_price);
    }
    //关闭文件
    fclose($fp);

    echo " </table>";

}



$html = <<<"HTML"
    <html>
       <link id='stylecss' type="text/css" rel="stylesheet" href="css/singleProduct.css">

</html>
HTML;





?>